// fpDateTime.cpp: implementation of the fpDateTime class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "fpDateTime.h"
#include "fpDateTimePeer.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#ifdef FPCONTROLS
#ifndef _UNICODE

IMPLEMENT_DYNAMIC(fpDateTime, AWTComponent)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

fpDateTime::fpDateTime()
{
	construct(CString(), FALSE, FALSE);
}

fpDateTime::fpDateTime(CString& text)
{
	construct(text, FALSE, FALSE);
}

fpDateTime::fpDateTime(CString& text, BOOL bUSA, BOOL bCentury)
{
	construct(text, bUSA, bCentury);
}

fpDateTime::~fpDateTime()
{
	if (m_actionListener != NULL)
	{
		if (m_actionListener->isMultiCaster())
		{
			AWTEventMulticaster* mc = dynamic_cast<AWTEventMulticaster*>(m_actionListener);
			delete mc;
		}
	}
}

CWnd* fpDateTime::createComponent()					 
{
	CWnd* wParent = getParent()->getPeer();
	if (wParent == NULL)
		return wParent;
	fpDateTimePeer* pwnd = new fpDateTimePeer(this);
	DWORD dwStyle = WS_CHILD | WS_VISIBLE;
	pwnd->Create(dwStyle, getBounds(), wParent, 0);
	pwnd->SetFont(CFont::FromHandle(m_font));
	pwnd->SetMarginTop(0);
	pwnd->SetMarginLeft(1);
	pwnd->SetAppearance(2);
	pwnd->SetButtonStyle(2);
	pwnd->SetButtonDefaultAction(TRUE);
	pwnd->SetDateTimeFormat(5);
	pwnd->SetUserEntry(0);
	pwnd->SetAllowNull(TRUE);
	pwnd->SetInvalidColor(RGB(255,255,255));
	pwnd->SetNullColor(RGB(255,255,255));
	TCHAR sz[128];
	if (m_bUSA)
	{
		if (m_bCentury)
			lstrcpy(sz, _T("mm/dd/yyyy"));
		else	
			lstrcpy(sz, _T("mm/dd/yy"));
	}
	else
	{
		if (m_bCentury)
			lstrcpy(sz, _T("dd/mm/yyyy"));
		else	
			lstrcpy(sz, _T("dd/mm/yy"));
	}
	pwnd->SetUserDefinedFormat(sz);
#ifndef _UNICODE
	pwnd->SetText(m_text.GetBuffer(0));
#endif
	AWTColor* c = getForeground();
	if (c != NULL)
		pwnd->SetForeColor(c->getColorRef());
	if (!m_enabled)
		pwnd->EnableWindow(FALSE);
	return pwnd;
}

CSize fpDateTime::preferredSize(int columns) 
{
	if (m_peer == NULL)
		return CSize(0,0);
	CSize sz(0,0);
	fpDateTimePeer* pwnd = (fpDateTimePeer*)m_peer;
	CDC* pdc = pwnd->GetDC();
	CFont* ft = CFont::FromHandle(m_font);
	CFont* ftOld = pdc->SelectObject(ft);
	TEXTMETRIC tm;
	pdc->GetTextMetrics(&tm);
	pdc->SelectObject(ftOld);
	pwnd->ReleaseDC(pdc);	
	sz.cx = (tm.tmAveCharWidth * columns) + 32;
	sz.cy = tm.tmHeight + 6;
	return sz;
}

CSize fpDateTime::preferredSize() 
{
	if (m_peer == NULL)
		return CSize(0,0);
	return preferredSize(m_columns);
}

CSize fpDateTime::minimumSize(int columns) 
{
	if (m_peer == NULL)
		return CSize(0,0);
	CSize sz(0,0);
	fpDateTimePeer* pwnd = (fpDateTimePeer*)m_peer;
	CDC* pdc = pwnd->GetDC();
	CFont* ft = CFont::FromHandle(m_font);
	CFont* ftOld = pdc->SelectObject(ft);
	TEXTMETRIC tm;
	pdc->GetTextMetrics(&tm);
	pdc->SelectObject(ftOld);
	pwnd->ReleaseDC(pdc);	
	sz.cx = (tm.tmAveCharWidth * columns) + 32;
	sz.cy = tm.tmHeight + 6;
	return sz;
}

CSize fpDateTime::minimumSize() 
{
	if (m_peer == NULL)
		return CSize(0,0);
	return minimumSize(m_columns);
}

void fpDateTime::setColumns(int columns)
{
	if (columns < 0) return;
	if (m_columns != columns)
	{
		m_columns = columns;
		invalidate();
	}
}

void fpDateTime::setText(CString& text)
{
	m_text = text;
	if (m_peer != NULL)
	{	
//		m_peer->PostMessage(WM_AWTSETTEXT, 0, (LPARAM)new CString(text));
#ifndef _UNICODE
		((fpDateTimePeer*)m_peer)->SetText(m_text.GetBuffer(0));	
#endif
	}
}

void fpDateTime::setText(LPTSTR text)
{
	if (text == NULL)
		m_text = CString();
	else
		m_text = text;
#ifndef _UNICODE
	if (m_peer != NULL)
		((fpDateTimePeer*)m_peer)->SetText(text);
#endif
}

CString& fpDateTime::getText()
{
	if (m_peer != NULL)
	{
		TCHAR sz[256];
		((fpDateTimePeer*)m_peer)->GetText(sz, sizeof(sz));
		m_text = sz;
	}
	return m_text;
}

void fpDateTime::setForeground(AWTColor* c)
{
	AWTComponent::setForeground(c);
	if (m_peer != NULL)
		((fpDateTimePeer*)m_peer)->SetForeColor(c->getColorRef());
}

#endif //_UNICODE
#endif //FPCONTROLD